/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelAyuda.java
 *
 * Created on 15/12/2008, 21:41:08
 */

package gui;

import controladores.ControladorAyuda;
import java.awt.Cursor;
import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;

/**
 *
 * @author demian
 */
public class PanelAyuda extends javax.swing.JPanel {
    private ControladorAyuda controlador;

    /** Creates new form PanelAyuda */
    public PanelAyuda() {
        controlador = new ControladorAyuda();
        initComponents();
    }

    /**
     * Buscar la ayuda de la palabra ingresada a travez del
     * controlador.
     *
     * @param ayudaDe palabra por la cual buscar ayuda.s
     */
    private void buscarAyudaDe(String ayudaDe) {
        if(!controlador.ayudaCargada()) {
            Cursor oldCursor = getCursor();

            try {
                this.jTextAreaTextoAyuda.setText("");
                setCursor(new Cursor(Cursor.WAIT_CURSOR));
                controlador.cargarAyuda();
                setCursor(oldCursor);
            } catch(Exception ex) {
                setCursor(oldCursor);
                String msg = ex.getMessage();
                JOptionPane.showMessageDialog(this, "No se pudo abrir la ayuda." + (msg == null ? "" : "\nRazon: " + msg));
            }
        }

        if(ayudaDe.isEmpty()) {
            JOptionPane.showMessageDialog(this, "Ingrese una palabra por la cual buscar.");
            this.jTextFieldBuscarAyudaDe.requestFocusInWindow();
            return;
        }

        ayudaDe = ayudaDe.trim();
        String ayuda = controlador.buscarAyudaDe(ayudaDe);
        if(ayuda == null) {
            JOptionPane.showMessageDialog(this, "No se encontro ayuda para '" + ayudaDe + "' porfavor intente con otra palabra.");
            this.jTextFieldBuscarAyudaDe.requestFocusInWindow();
            this.jTextFieldBuscarAyudaDe.setSelectionStart(0);
            this.jTextFieldBuscarAyudaDe.setSelectionEnd(ayudaDe.length());
            return;
        }

        this.jTextAreaTextoAyuda.setText(ayuda);
        this.jTextFieldBuscarAyudaDe.requestFocusInWindow();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextFieldBuscarAyudaDe = new javax.swing.JTextField();
        jButtonBuscar = new javax.swing.JButton();
        jScrollPaneTextoAyuda = new javax.swing.JScrollPane();
        jTextAreaTextoAyuda = new javax.swing.JTextArea();

        jLabel1.setText("Buscar ayuda sobre:");

        jTextFieldBuscarAyudaDe.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextFieldBuscarAyudaDeKeyPressed(evt);
            }
        });

        jButtonBuscar.setText("Buscar");
        jButtonBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonBuscarActionPerformed(evt);
            }
        });

        jTextAreaTextoAyuda.setColumns(20);
        jTextAreaTextoAyuda.setEditable(false);
        jTextAreaTextoAyuda.setRows(5);
        jScrollPaneTextoAyuda.setViewportView(jTextAreaTextoAyuda);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPaneTextoAyuda, javax.swing.GroupLayout.DEFAULT_SIZE, 662, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldBuscarAyudaDe, javax.swing.GroupLayout.PREFERRED_SIZE, 305, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonBuscar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldBuscarAyudaDe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonBuscar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPaneTextoAyuda, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBuscarActionPerformed
        buscarAyudaDe(this.jTextFieldBuscarAyudaDe.getText());
    }//GEN-LAST:event_jButtonBuscarActionPerformed

    private void jTextFieldBuscarAyudaDeKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextFieldBuscarAyudaDeKeyPressed
        if(evt.getKeyCode() == KeyEvent.VK_ENTER) {
            buscarAyudaDe(this.jTextFieldBuscarAyudaDe.getText());
        }
    }//GEN-LAST:event_jTextFieldBuscarAyudaDeKeyPressed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonBuscar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPaneTextoAyuda;
    private javax.swing.JTextArea jTextAreaTextoAyuda;
    private javax.swing.JTextField jTextFieldBuscarAyudaDe;
    // End of variables declaration//GEN-END:variables
}
